package pers.qianyu.module.image.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.qianyu.module.core.comm.HttpResult;
import pers.qianyu.module.core.comm.Pagination;
import pers.qianyu.module.core.domain.image.dto.ImageDTO;
import pers.qianyu.module.core.domain.image.query.FollowedUserImageQuery;
import pers.qianyu.module.core.domain.image.query.ImageQuery;
import pers.qianyu.module.core.domain.image.query.KeywordsQuery;
import pers.qianyu.module.core.domain.image.vo.ImageVO;
import pers.qianyu.module.image.service.ImageService;
import pers.qianyu.module.logging.annotation.ControllerLog;
import pers.qianyu.module.logging.enums.BusinessType;

import java.util.List;

/**
 * @author mizzle rain
 * @date 2020-12-05 15:28
 */
@Api(tags = "图片操作相关接口")
@RequestMapping("/image-serv")
@RestController
public class ImageController {
    @Autowired
    private ImageService imageService;

    @ApiOperation("根据图片ID查询图片信息")
    @ControllerLog(businessType = BusinessType.QUERY)
    @GetMapping("/image/{id}")
    public HttpResult<ImageVO> queryById(@PathVariable Long id) {
        ImageVO imageVO = imageService.queryById(id);
        return HttpResult.okWithData(imageVO);
    }

    @ApiOperation("插入图片")
    @ControllerLog(businessType = BusinessType.ADD)
    @PostMapping("/image")
    public HttpResult<?> insert(@RequestBody ImageDTO imageDTO) {
        Long id = imageService.insImage(imageDTO);
        HttpResult<?> httpResult = HttpResult.okWithData(id);
        httpResult.setMessage("新增图片成功");
        return httpResult;
    }

    @ApiOperation("根据图片ID删除图片")
    @ControllerLog(businessType = BusinessType.DELETE)
    @DeleteMapping("/image/{id}")
    public HttpResult<?> delete(@PathVariable Long id) {
        imageService.delImage(id);
        return HttpResult.okWithMessage("删除图片成功");
    }

    @ApiOperation("根据图片ID修改图片")
    @ControllerLog(businessType = BusinessType.UPDATE)
    @PutMapping("/image/{id}")
    public HttpResult<?> updateById(@PathVariable Long id,
                                    @RequestBody ImageDTO imageDTO) {
        imageService.updImage(id, imageDTO);
        return HttpResult.okWithMessage("更新成功");
    }

    @ApiOperation("根据不同查询条件查询图片信息")
    @ControllerLog(businessType = BusinessType.QUERY)
    @GetMapping("/images")
    public HttpResult<Pagination<ImageVO>> query(ImageQuery imageQuery) {
        Pagination<ImageVO> page = imageService.queryImage(imageQuery);
        return HttpResult.okWithData(page);
    }

    @ApiOperation("根据图片标题关键词查询图片信息")
    @ControllerLog(businessType = BusinessType.QUERY)
    @GetMapping("/images_keyword")
    public HttpResult<Pagination<ImageVO>> queryByKeywords(KeywordsQuery keywordsQuery) {
        Pagination<ImageVO> page = imageService.queryImageByKeywords(keywordsQuery);
        return HttpResult.okWithData(page);
    }

    @ApiOperation("查询图片日榜信息")
    @ControllerLog(businessType = BusinessType.QUERY)
    @GetMapping("/daily_rank")
    public HttpResult<List<ImageVO>> queryDailyRank() {
        List<ImageVO> page = imageService.queryDailyRank(0, 10);
        return HttpResult.okWithData(page);
    }

    @ApiOperation("分页查询关注的人发布的图片信息")
    @ControllerLog(businessType = BusinessType.QUERY)
    @GetMapping("/images_follow")
    public HttpResult<Pagination<ImageVO>> getFollowedUserImages(FollowedUserImageQuery followedUserImageQuery) {
        Pagination<ImageVO> page = imageService.getFollowedUserImages(followedUserImageQuery);
        return HttpResult.okWithData(page);
    }
}
